import React, { Fragment } from 'react';
import cookie from 'react-cookies';
import { autoLogin } from '../api/store';
import { setAuto } from '../store/actives/actives';
import { connect } from 'react-redux';

const RouterConfig = (props) => {
  let urlText = props.location.pathname;
  let loginStore = props.store.loginStore;
  if (!urlText.includes('login') && !loginStore) {
    let userId = cookie.load('userId');
    if (userId) {
      autoLogin({ token: userId }).then((res) => {
        if (res.code === 2000) {
          let { token, visitList } = res;
          props.setAutoLogin({ token, visitList });
        } else {
          props.history.replace('/login');
        }
      });
    } else {
      props.history.replace('/login');
    }
  }
  return <Fragment></Fragment>;
};
let mapStoreToProps = (store) => {
  return {
    store,
  };
};

let mapDisplayToProps = (display) => {
  return {
    setAutoLogin(data) {
      display(setAuto(data));
    },
  };
};

export default connect(mapStoreToProps, mapDisplayToProps)(RouterConfig);
